package com.itheima.unusualDemo.studentDemo;

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Student s = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名");
        String name = sc.nextLine();
        s.setName(name);


        while (true) {
            System.out.println("请输入学生年龄");
            String ageStr = sc.nextLine();
            try {
                //会出现类型转换异常; NumberFormatException
                int age = Integer.parseInt(ageStr);
                s.setAge(age);
                break;
                //如果try中能执行到这里,说明了没有出现异常,此时对象复制完成,结束循环
            } catch (NumberFormatException e) {
                e.printStackTrace();
                System.out.println("警告:只能输入数字年龄");
            } catch (RuntimeException e) {
                System.out.println(e.getMessage());
                System.out.println("警告:年龄应在18~25岁之间");
            }
        }
        System.out.println("----------------------------");
        System.out.println("姓名:" + s.getName());
        System.out.println("年龄:" + s.getAge());
    }
}
